package org.jeecg.modules.workflow.convert.definition;

import com.fasterxml.jackson.core.type.TypeReference;
import org.flowable.engine.repository.ProcessDefinition;
import org.jeecg.common.util.json.JsonUtils;
import org.jeecg.common.workflow.vo.definition.process.BpmProcessDefinitionPageItemRespVO;
import org.jeecg.common.workflow.vo.definition.process.BpmProcessDefinitionRespVO;
import org.jeecg.modules.workflow.entity.BpmProcessDefinitionExt;
import org.jeecg.modules.workflow.service.definition.dto.BpmProcessDefinitionCreateReqDTO;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class BpmProcessDefinitionConvertImpl implements BpmProcessDefinitionConvert {
    private static final TypeReference<List<String>> typeReference = new TypeReference<List<String>>(){};

    @Override
    public BpmProcessDefinitionPageItemRespVO convert(ProcessDefinition bean) {
        if ( bean == null ) {
            return null;
        }

        BpmProcessDefinitionPageItemRespVO bpmProcessDefinitionPageItemRespVO = new BpmProcessDefinitionPageItemRespVO();

        bpmProcessDefinitionPageItemRespVO.setId( bean.getId() );
        bpmProcessDefinitionPageItemRespVO.setVersion( bean.getVersion() );
        bpmProcessDefinitionPageItemRespVO.setName( bean.getName() );
        bpmProcessDefinitionPageItemRespVO.setDescription( bean.getDescription() );
        bpmProcessDefinitionPageItemRespVO.setCategory( bean.getCategory() );

        return bpmProcessDefinitionPageItemRespVO;
    }

    @Override
    public BpmProcessDefinitionExt convert2(BpmProcessDefinitionCreateReqDTO bean) {
        if ( bean == null ) {
            return null;
        }

        BpmProcessDefinitionExt bpmProcessDefinitionExtDO = new BpmProcessDefinitionExt();

        bpmProcessDefinitionExtDO.setModelId( bean.getModelId() );
        bpmProcessDefinitionExtDO.setDescription( bean.getDescription() );
        bpmProcessDefinitionExtDO.setFormType( bean.getFormType() );
        bpmProcessDefinitionExtDO.setFormId( bean.getFormId() );
        bpmProcessDefinitionExtDO.setFormConf( bean.getFormConf() );
        List<String> list = bean.getFormFields();
        if ( list != null ) {
            bpmProcessDefinitionExtDO.setFormFields( JsonUtils.toJsonString(list));
        }
        bpmProcessDefinitionExtDO.setFormCustomCreatePath( bean.getFormCustomCreatePath() );
        bpmProcessDefinitionExtDO.setFormCustomViewPath( bean.getFormCustomViewPath() );
        bpmProcessDefinitionExtDO.setFormCanReturn(bean.getFormCanReturn());
        return bpmProcessDefinitionExtDO;
    }

    @Override
    public BpmProcessDefinitionRespVO convert3(ProcessDefinition bean) {
        if ( bean == null ) {
            return null;
        }

        BpmProcessDefinitionRespVO bpmProcessDefinitionRespVO = new BpmProcessDefinitionRespVO();

        bpmProcessDefinitionRespVO.setSuspensionState( convertSuspendedToSuspensionState( bean.isSuspended() ) );
        bpmProcessDefinitionRespVO.setId( bean.getId() );
        bpmProcessDefinitionRespVO.setVersion( bean.getVersion() );
        bpmProcessDefinitionRespVO.setName( bean.getName() );
        bpmProcessDefinitionRespVO.setDescription( bean.getDescription() );
        bpmProcessDefinitionRespVO.setCategory( bean.getCategory() );

        return bpmProcessDefinitionRespVO;
    }

    @Override
    public void copyTo(BpmProcessDefinitionExt from, BpmProcessDefinitionRespVO to) {
        if ( from == null ) {
            return;
        }

        to.setDescription( from.getDescription() );
        to.setFormType( from.getFormType() );
        to.setFormId( from.getFormId() );
        to.setFormConf( from.getFormConf() );
        if ( to.getFormFields() != null ) {
            List<String> list = JsonUtils.parseObject(from.getFormFields(), typeReference);
            if ( list != null ) {
                to.getFormFields().clear();
                to.getFormFields().addAll( list );
            }
            else {
                to.setFormFields( null );
            }
        }
        else {
            if(from.getFormFields() != null){
                List<String> list = JsonUtils.parseObject(from.getFormFields(), typeReference);
                if ( list != null ) {
                    to.setFormFields( new ArrayList<String>( list ) );
                }
            }
        }
        to.setFormCustomCreatePath( from.getFormCustomCreatePath() );
        to.setFormCustomViewPath( from.getFormCustomViewPath() );
    }
}
